package com.lzy.mall.ware.controller;


import cn.hutool.core.collection.CollUtil;
import com.lzy.mall.common.exception.CommonError;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.ware.entity.WmsWareSku;
import com.lzy.mall.ware.service.WmsWareSkuService;
import com.lzy.mall.common.vo.SkuStockVO;
import com.lzy.mall.ware.vo.WareSkuLockVo;
import io.renren.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 商品库存(WmsWareSku)表控制层
 *
 * @author lzy
 * @since 2021-08-24 19:02:02
 */
@RestController
@RequestMapping("waresku")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class WmsWareSkuController {
    private final WmsWareSkuService wareSkuService;

    @PostMapping("/lock/order")
    ResultUtils orderLockStock(@RequestBody WareSkuLockVo vo){
        try {
            wareSkuService.orderLockStock(vo);
            return ResultUtils.ok();
        } catch (RRException e) {
            log.warn("\n" + e.getMessage());
        }
        return ResultUtils.error(CommonError.NOT_STOCK_EXCEPTION);

    }

    /**
     * 查询sku是否有库存
     */
    @PostMapping("/stock")
    public List<SkuStockVO> getSkuStock(@RequestBody List<Long> skuIds){
        List<SkuStockVO> vos = wareSkuService.getSkuStock(skuIds);
        log.info("ware返回数据：{}", vos);
        return vos;
    }

    @GetMapping("/list")
    public ResultUtils list(@RequestParam Map<String,Object> params){
        PageUtils page = wareSkuService.queryPage(params);
        return ResultUtils.ok().put("page",page);
    }

    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:wareinfo:info")
    public ResultUtils info(@PathVariable("id") Long id){
        WmsWareSku wareSku = wareSkuService.getById(id);

        return ResultUtils.ok().put("wareSku", wareSku);
    }

    @PostMapping("/save")
    public ResultUtils save(@RequestBody WmsWareSku wareSku){
        wareSkuService.save(wareSku);
        return ResultUtils.ok();
    }

    @PostMapping("/update")
    public ResultUtils update(@RequestBody WmsWareSku wareSku){
        wareSkuService.update(wareSku);
        return ResultUtils.ok();
    }

    @PostMapping("/delete")
    public ResultUtils delete(@RequestBody Long[] ids){
        wareSkuService.removeBatch(CollUtil.toList(ids));
        return ResultUtils.ok();
    }
}
